package br.ucb.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.ucb.connection.FactoryConnection;
import br.ucb.entity.Conceito;
import br.ucb.entity.Disciplina;


public class ConceitoDAO {
	
	/* Insere Conceito */
	public static void insertConceito(Conceito conceito)throws SQLException, ClassNotFoundException {
		Connection con = FactoryConnection.getInstance().criaConexao();
		PreparedStatement ps = null;
		String SQL_INSERT = "INSERT INTO Conceito(nomeConceito,idDisciplina,idConceitoPai,idDisciplinaPai)" +
		"values(?,?,?,?);";

		ps = con.prepareStatement(SQL_INSERT);
		ps.setString(1,conceito.getNomeConceito());
		ps.setInt(2,conceito.getIdDisciplina());
		ps.setString(3,conceito.getIdConceitoPai());
		ps.setString(4,conceito.getIdDisciplinaPai());
		
		
		ps.executeUpdate();
		
		con.close();
		ps.close();
	}
	
	// Busca os conceitos do Banco
	public static List<Conceito> getConceitos() throws SQLException, ClassNotFoundException{
		List<Conceito> conceitos = new ArrayList<Conceito>();
		Disciplina disciplina = DisciplinaDAO.getDisciplinaByLastId();
		String SQL_SELECT = "SELECT * FROM Conceito where idDisciplina = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_SELECT);
		ps.setInt(1,disciplina.getIdDisciplina());
		rs = ps.executeQuery();
		while(rs.next()){
			/*idConceito int(6) NOT NULL AUTO_INCREMENT,
			  nomeConceito varchar(50) NOT NULL,
			  idDisciplina int(6) NOT NULL,
			  idConceitoPai int(6),
			  idDisciplinaPai int(6),
			public Conceito(Integer idConceito, Integer idDisciplina,
					String idConceitoPai, String idDisciplinaPai, String nomeConceito) */
			conceitos.add(new Conceito(rs.getInt(1),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getString(2)));
		}
		ps.close();
		if(conceitos.size() == 0)
			return null;
		return conceitos;
		
	}
	
	//Busca o Conceito pelo Nome
	public static Integer getIdConceitoByName(String nome) throws SQLException, ClassNotFoundException{
		
		Integer id = 0;
		String SQL_SELECT = "SELECT idConceito FROM Conceito where nomeConceito = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_SELECT);
		ps.setString(1,nome);
		rs = ps.executeQuery();
		while(rs.next()){
			/*idConceito int(6) NOT NULL AUTO_INCREMENT,
			  nomeConceito varchar(50) NOT NULL,
			  idDisciplina int(6) NOT NULL,
			  idConceitoPai int(6),
			  idDisciplinaPai int(6),
			public Conceito(Integer idConceito, Integer idDisciplina,
					String idConceitoPai, String idDisciplinaPai, String nomeConceito) */
			id = rs.getInt(1);
		}
		ps.close();
		return id;
		
	}
	public static String getNameByIdConceito(Integer idConceito)
			throws SQLException, ClassNotFoundException {

		String nome = null;
		String SQL_SELECT = "SELECT nomeConceito FROM Conceito where idConceito = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = FactoryConnection.getInstance().criaConexao()
				.prepareStatement(SQL_SELECT);
		ps.setInt(1, idConceito);
		rs = ps.executeQuery();
		while (rs.next()) {
			nome = rs.getString(1);
		}
		ps.close();
		return nome;

	}
	/*public static List<Bolao> findNumConcurso(Integer numConcurso) throws SQLException, ClassNotFoundException{
		List<Bolao> boloes = new ArrayList<Bolao>();
		String SQL_SELECT = "select * from boloes where num_Concurso = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_SELECT);
		ps.setInt(1,numConcurso);
		rs = ps.executeQuery();
		while(rs.next()){
			boloes.add(new Bolao(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDouble(4)));
		}
		ps.close();
	//	FactoryConnection.close();
		if(boloes.size() == 0)
			return null;
		return boloes;
	}
	 * 
	 * 
	 */
	
	
	/* Seleciona todos os boloes */
/*	public static List<Bolao> selectAll()throws SQLException, ClassNotFoundException{
		List<Bolao> bolao = new ArrayList<Bolao>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String SQL_SELECT = "select * from boloes;";
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_SELECT);
		rs = ps.executeQuery();
		while(rs.next()){
			bolao.add(new Bolao(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDouble(4)));
		}
	//	FactoryConnection.close();
		return bolao;
	}

	
	public static boolean findById(Integer id_bolao)throws SQLException, ClassNotFoundException{
		Bolao bolao;
		Connection con = FactoryConnection.getInstance().criaConexao();
		String SQL_SELECT = "SELECT * FROM boloes WHERE id_bolao = ?;";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = con.prepareStatement(SQL_SELECT);
		ps.setInt(1,id_bolao);
		rs = ps.executeQuery();
		while(rs.next()){
			ps.close();
			con.close();
			return true;
		}
		ps.close();
		con.close();
		return false;
	}
	// Procura bolao por id 
	public static Bolao getById(Integer id_bolao)throws SQLException, ClassNotFoundException{
		Bolao bolao = null;
		Connection con = FactoryConnection.getInstance().criaConexao();
		String SQL_SELECT = "SELECT * FROM boloes WHERE id_bolao = ?;";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = con.prepareStatement(SQL_SELECT);
		ps.setInt(1,id_bolao);
		rs = ps.executeQuery();
		while(rs.next()){
			bolao = new Bolao(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDouble(4));
		}
		ps.close();
		con.close();
		return bolao;
	}
	// Procura bolao por numero de concurso
	public static List<Bolao> findNumConcurso(Integer numConcurso) throws SQLException, ClassNotFoundException{
		List<Bolao> boloes = new ArrayList<Bolao>();
		String SQL_SELECT = "select * from boloes where num_Concurso = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_SELECT);
		ps.setInt(1,numConcurso);
		rs = ps.executeQuery();
		while(rs.next()){
			boloes.add(new Bolao(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDouble(4)));
		}
		ps.close();
	//	FactoryConnection.close();
		if(boloes.size() == 0)
			return null;
		return boloes;
	}
	
	//Deleta Bolao 
	public static void deletaBolao(Integer idBolao)throws SQLException, ClassNotFoundException{
		String SQL_DELETE =  "DELETE FROM boloes WHERE id_bolao = ?;";
		PreparedStatement ps = null;
		ps = FactoryConnection.getInstance().criaConexao().prepareStatement(SQL_DELETE);
		ps.setInt(1,idBolao);
		ps.executeUpdate();
	//	FactoryConnection.close();	
		ps.close();
	}

	public static Bolao getById(Integer idBolao,Integer numConcurso) throws ClassNotFoundException, SQLException {
		Bolao bolao = null;
		Connection con = FactoryConnection.getInstance().criaConexao();
		String SQL_SELECT = "SELECT * FROM boloes WHERE id_bolao = ? and num_concurso = ?;";
		PreparedStatement ps = null;
		ResultSet rs = null;
		ps = con.prepareStatement(SQL_SELECT);
		ps.setInt(1,idBolao);
		ps.setInt(2,numConcurso);
		rs = ps.executeQuery();
		while(rs.next()){
			bolao = new Bolao(rs.getInt(1),rs.getString(2), rs.getInt(3),rs.getDouble(4));
		}
		ps.close();
		con.close();
		return bolao;
	}*/
}
